#!/usr/bin/env python
import sys
from lib.format_time import *
from lib.HandleMysql import *
from lib.switch_code import *
from shop_conn import *
import sys
import re
import os
sys.setdefaultencoding('utf-8')

def get_category(cate_id ):
    stat_conn = set_stats_conn()
    query = 'select fullpath from t_category where cateid =%s' % (cate_id)
    #print query
    items = stat_conn.get_results(query)
    if len(items) == 0:
        return None
    
    fullpath = items[0][0]        
    q = "select cateid, catename from t_category where cateid<>895 and cateid in (%s) order by depth asc" % fullpath
    rt = stat_conn.get_results(q)
    
    stat_conn.close()
    return rt

def get_full_category(stat_conn,cate_id ):
    #stat_conn = set_stats_conn()
    query = 'select fullpath from t_category where cateid =%s' % (cate_id)
    #print query
    items = stat_conn.get_results(query)
    if len(items) == 0:
        return None
    
    fullpath = items[0][0]        
    q = "select cateid, catename from t_category where cateid<>895 and cateid in (%s) order by depth asc" % fullpath
    rt = stat_conn.get_results(q)
    
    #stat_conn.close()
    return rt

def get_second_cat(stat_conn,cate_id ):
    #stat_conn = set_stats_conn()
    query = 'select fullpath from t_dispcategory where cateid =%s and depth=1;' % (cate_id)
    #print query
    items = stat_conn.get_results(query)
    if len(items) == 0:
        q = "select cateid, catename from t_category where cateid=%s" % cate_id
        rt = stat_conn.get_results(q)
        return rt
    
    fullpath = items[0][0]        
    q = "select dispcategoryid, catename from t_dispcategory where dispcategoryid in (%s) order by depth asc" % fullpath
    rt = stat_conn.get_results(q)
    
    #stat_conn.close()
    return rt

def get_thirth_cat(cate_id ,param):    
    stat_conn = set_stats_conn()
    query = 'select param_id from t_cate_param where cate_id = %s;' % cate_id
    items = stat_conn.get_results(query)
    if len(items) == 0:
        return get_second_cat(stat_conn,cate_id )
    param_id = items[0][0]
    reg = '{"I":%s,"V":"(\w*)"}'%param_id
    #print reg
    pattern =  re.compile(reg)
    matchs = pattern.search(param)
    #print reg
    objval = "0"
    #print matchs
    try:
        objval = matchs.group(1)
    except Exception,e:
        return get_second_cat(stat_conn,cate_id )
        
    filter_str = "objecttype=%s"%objval
    #print filter_str
    query = "select fullpath from t_dispcategory where cateid =%s and filter='%s'" % (cate_id,filter_str)
    items = stat_conn.get_results(query)
    if len(items) == 0:
        return get_second_cat(stat_conn,cate_id )
    fullpath = items[0][0]      
    q = "select dispcategoryid, catename from t_dispcategory where dispcategoryid in (%s) order by depth asc" % fullpath
    rt = stat_conn.get_results(q)
    
    stat_conn.close()
    
    return rt
def get_citys(city):
    if city == 0 :
        return None
    
    stat_conn = set_stats_conn()
    q = 'select fullpath from t_local where localid=%s' % city
    #print q
    items = stat_conn.get_results(q)
    if len(items) == 0:
        return None
    #depth = items[0][0]
    #if depth == 0:
    #    return items[0][2],items[0][3]
    
    fullpath = items[0][0]
    #print full
    q = "select localid, localname from t_local where localid in (%s) order by depth asc " % fullpath
    #print q
    rt = stat_conn.get_results(q)
    stat_conn.close()
    return rt

def get_full_citys(stat_conn,city):
    if city == 0 :
        return None
    
    #stat_conn = set_stats_conn()
    q = 'select fullpath from t_local where localid=%s' % city
    #print q
    items = stat_conn.get_results(q)
    if len(items) == 0:
        return None
    #depth = items[0][0]
    #if depth == 0:
    #    return items[0][2],items[0][3]
    
    fullpath = items[0][0]
    #print full
    q = "select localid, localname from t_local where localid in (%s) order by depth asc " % fullpath
    #print q
    rt = stat_conn.get_results(q)
    #stat_conn.close()
    return rt

def get_city(city):
    if city == 0 :
        return 0,''
    
    stat_conn = set_stats_conn()
    q = 'select depth,fullpath,localid,localname from t_local where localid=%s' % city
    items = stat_conn.get_results(q)
    if len(items) == 0:
        return 0,''
    depth = items[0][0]
    if depth == 0:
        return items[0][2],items[0][3]
    
    fullpath = items[0][1]
    q = "select localid, localname from t_local where localid in (%s) order by depth asc limit 1" % fullpath
    #print q
    rt = stat_conn.get_results(q)
    if len(rt) == 0:
        return 0,''
    stat_conn.close()
    return rt[0][0],rt[0][1]
